package designPattern;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * @BelongsPackage: designPattern
 * @Author: linqh03
 * @CreateTime: 2021-09-14 16:25
 * @Description: 使用java的Observable、Observer实现观察者模式
 */
public class ObserverModelTest2 {
    public static void main(String[] args) {
        Observer abstractConverserA= new ConcreteConverser2A();
        Observer abstractConverserB= new ConcreteConverser2B();
        Observable target = new ConcreteTarget2();
        target.addObserver(abstractConverserA);
        target.addObserver(abstractConverserB);
        target.notifyObservers("test");
    }
}


//具体目标
class ConcreteTarget2 extends Observable {
    @Override
    public void notifyObservers(Object msg) {
        super.setChanged();//设置启动，后面的notifyObservers才能执行
        super.notifyObservers(msg);
    }
}



//具体观察者A
class ConcreteConverser2A implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        System.out.println("观察者A收到变更通知:"+arg);
    }
}

//具体观察者B
class ConcreteConverser2B implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        System.out.println("观察者B收到变更通知:"+arg);
    }
}

